<template>
  <el-upload
    class="mg-l"
    accept=""
    :headers="headers"
    :action="upload_url"
    :on-success="excelUploadSuccess"
    :on-error="excelUploadFail"
    :show-file-list="false"
    :on-progress="progress"
  >
    <slot></slot>
  </el-upload>
</template>

<script>
  import { headers } from "../../utils/request";
  import xlsx from "xlsx";

  export default {
    components: {},
    props: {
      text:String,
      http_api:{
        default: "VUE_APP_API"
      },
      http_url:{
        default: "/v1/ad/upload_excel"
      }
    },
    data() {
      return {
        headers: headers(),
        clipboard:null,
        upload_url:""
      };
    },
    computed: {
    },
    mounted() {
      this.upload_url = import.meta.env[this.http_api] + this.http_url;
    },
    methods: {
      async progress(event,file){
      },
      excelUploadFail(err) {
        this.$message(JSON.parse(err.message));
        this.$emit("onError",err)
      },
      async excelUploadSuccess(res,file) {
        this.$message("导入成功");
        this.$emit("onSuccess",res,file)
        
      },

      readFile(file) {
        return new Promise(resolve => {
          let reader = new FileReader()
          reader.readAsBinaryString(file)
          reader.onload = ev => {
            resolve(ev.target.result)
          }
        })
      }
    }
  };
</script>
<style scoped lang='less'>

</style>
<style lang='less'>

</style>